//
//  FriendCell.m
//  SportSocial
//
//  Created by yull on 14/12/24.
//  Copyright (c) 2014年 yull. All rights reserved.
//

#import "FriendCell.h"
#import "UIImageView+WebCache.h"

@implementation FriendCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        
        CGRect rect = CGRectMake(10, 15, 40, 40);
        self.headImageView = [[UIImageView alloc] initWithFrame:rect];
        self.headImageView.layer.cornerRadius = self.headImageView.height/2;
        self.headImageView.layer.masksToBounds = YES;
        self.headImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:self.headImageView];
        
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onClickHeadImageView:)];
        [self.headImageView addGestureRecognizer:tapGesture];
        
        CGFloat orginX = self.headImageView.right + 10;
        rect = CGRectMake(orginX, 10, self.contentView.width - orginX - 10, 20);
        self.nameLabel = [[UILabel alloc] initWithFrame:rect];
        self.nameLabel.backgroundColor = [UIColor whiteColor];
        self.nameLabel.font = [UIFont boldSystemFontOfSize:18.0f];
        self.nameLabel.textColor = [UIColor blackColor];
        self.nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.nameLabel];
        
        rect = CGRectMake(0, self.nameLabel.top + 2, 16, 16);
        self.sexImageView = [[UIImageView alloc] initWithFrame:rect];
        [self.contentView addSubview:self.sexImageView];
        
        rect = self.nameLabel.frame;
        rect.origin.y = self.nameLabel.bottom + 10;
        self.signLabel = [[UILabel alloc] initWithFrame:rect];
        self.signLabel.backgroundColor = [UIColor clearColor];
        self.signLabel.font = [UIFont systemFontOfSize:16.0f];
        self.signLabel.textColor = [UIColor grayColor];
        self.signLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        [self.contentView addSubview:self.signLabel];
    }
    return self;
}

+ (CGFloat)friendCellDefaultHeight
{
    return 70.0f;
}

- (void)drawCellWithFriendInfo:(UserModel *)friendInfo
{
    self.friendInfo = friendInfo;
    
    [self.headImageView sd_setImageWithURL:[NSURL URLWithString:friendInfo.icon] placeholderImage:[UIImage imageNamed:@"zc_photo_bg"]];
    
    self.nameLabel.text = friendInfo.name;
    
    if (friendInfo.signature.length == 0) {
        self.signLabel.text = @"他很懒,什么都没有写...";
    }else {
        self.signLabel.text = friendInfo.signature;
    }
    
    NSString *imageName = friendInfo.sex == 1 ? @"female_icon" : @"male_icon";
    self.sexImageView.image = [UIImage imageNamed:imageName];
    
    [self.nameLabel sizeToFit];
    
    self.sexImageView.left = self.nameLabel.right + 5;
}

- (UIEdgeInsets)layoutMargins
{
    return UIEdgeInsetsZero;
}

- (void)onClickHeadImageView:(UIGestureRecognizer *)gesture
{
    if ([self.delegate respondsToSelector:@selector(onClickHeadImageViewWithFriendInfo:)]) {
        [self.delegate onClickHeadImageViewWithFriendInfo:self.friendInfo];
    }
}
@end
